package com.xmwzw.catpu.util;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.xmwzw.catpu.BaseApplication;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import static android.os.Build.VERSION;
import static android.os.Build.VERSION_CODES;

/**
 * 功能描述:Activity统一管理工具类
 * Created by  on 2016/7/27.
 */

/**
 * 描述: Activity 管理工具类( 需要android 4.0系统(14) )
 * <p>
 * created: 2018/7/4/004 10:39
 **/

public class ActivityManagerUtils {

    private static ActivityManagerUtils activityManagerUtils;

    private ActivityManagerUtils() {
    }

    public static ActivityManagerUtils getInstance() {

        if (activityManagerUtils == null) {

            synchronized (ActivityManagerUtils.class) {
                if (activityManagerUtils == null) {
                    activityManagerUtils = new ActivityManagerUtils();
                }
            }
        }
        return activityManagerUtils;
    }


    /**
     * 管理Activity的list集合
     */
    private static List<Activity> mActivitys = Collections.synchronizedList(new LinkedList<Activity>());

    public List<Activity> getmActivitys() {

        return mActivitys;
    }

    /**
     * 添加一个activity到list里
     */
    public void pushActivity(Activity activity) {

        if (mActivitys == null) {

            return;
        }

        mActivitys.add(activity);

    }


    /**
     * 从list里删除一个activity
     */
    public void popActivity(Activity activity) {

        if (mActivitys == null) {
            return;
        }

        mActivitys.remove(activity);

    }


    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {

        if (mActivitys == null || mActivitys.isEmpty()) {
            return;
        }

        List<Activity> activityList = new ArrayList<>();

        for (Activity activity : mActivitys) {

            if (activity != null && activity.getClass().equals(cls)) {

                activityList.add(activity);
            }
        }

        mActivitys.removeAll(activityList);

        for (int i = 0; i < activityList.size(); i++) {

            Activity activity = activityList.get(i);

            if (activity != null) {
                activity.finish();
            }
        }
    }


    /**
     * 结束指定类名的 Activity 集合
     */
    public void finishActivityCollection(@NonNull List<Class<?>> classList) {

        if (mActivitys == null || mActivitys.isEmpty()) {
            return;
        }

        List<Activity> needFinishedActivityList = new ArrayList<>();

        for (int i = 0; i < classList.size(); i++) {

            Class<?> aClass = classList.get(i);

            for (Activity activity : mActivitys) {

                if (activity != null && activity.getClass().equals(aClass)) {

                    if (!needFinishedActivityList.contains(activity)) {
                        needFinishedActivityList.add(activity);
                    }
                }
            }
        }

        mActivitys.removeAll(needFinishedActivityList);

        for (int j = 0; j < needFinishedActivityList.size(); j++) {

            Activity activity = needFinishedActivityList.get(j);

            if (activity != null) {
                activity.finish();
            }
        }
    }


    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {


        if (mActivitys == null) {
            return;
        }


        List<Activity> needFinishedActivityList = new ArrayList<>();

        for (int i = 0; i < mActivitys.size(); i++) {

            for (Activity activity : mActivitys) {

                if (activity != null) {

                    if (!needFinishedActivityList.contains(activity)) {
                        needFinishedActivityList.add(activity);
                    }
                }
            }
        }

        mActivitys.removeAll(needFinishedActivityList);

        for (int j = 0; j < needFinishedActivityList.size(); j++) {

            Activity activity = needFinishedActivityList.get(j);

            if (activity != null) {
                activity.finish();
            }
        }
    }


    public void registerActivityListener() {

        if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) {

            BaseApplication.getInstance().registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
                @Override
                public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

                    pushActivity(activity);

                }

                @Override
                public void onActivityStarted(Activity activity) {

                }

                @Override
                public void onActivityResumed(Activity activity) {

                }

                @Override
                public void onActivityPaused(Activity activity) {

                }

                @Override
                public void onActivityStopped(Activity activity) {

                }

                @Override
                public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

                }

                @Override
                public void onActivityDestroyed(Activity activity) {

                    if (null == mActivitys || mActivitys.isEmpty()) {
                        return;
                    }

                    if (mActivitys.contains(activity)) {

                        popActivity(activity);
                    }
                }
            });
        }
    }

}

